﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace chungcu
{
    public partial class quanlyphong : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("phong");
        private SqlDataAdapter da = new SqlDataAdapter();

        private void connect()
        {

            String cn = "Data source=(local);Initial Catalog=Quanlychungcu;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open(); //mo ket noi

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close(); //dong ket noi
            conn.Dispose(); //giai phong tai nguyen
            conn = null; //huy doi tuong
        }
        public quanlyphong()
        {
            InitializeComponent();
        }

        private void quanlyphong_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            binding();
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select  pid[Số phòng],ptang[Tầng],pdientich[Diện tích],phuongnha[Hướng nhà],pthongtin[Thông tin] from phong"; 
                       
            da.SelectCommand = command;
            da.Fill(dt);
            showphong.DataSource = dt;
        }

        private void binding()
        {

            txtsophong.DataBindings.Clear();
            txtsophong.DataBindings.Add("Text", showphong.DataSource, "Số phòng");
            txtsotang.DataBindings.Clear();
            txtsotang.DataBindings.Add("Text", showphong.DataSource, "Tầng");
            txtdientich.DataBindings.Clear();
            txtdientich.DataBindings.Add("Text", showphong.DataSource, "Diện tích");
            txthuongnha.DataBindings.Clear();
            txthuongnha.DataBindings.Add("Text", showphong.DataSource, "Hướng nhà");
            txtthongtin.DataBindings.Clear();
            txtthongtin.DataBindings.Add("Text", showphong.DataSource, "Thông tin");
            
            
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            
        }

        private void btnnhapphong_Click(object sender, EventArgs e)
        {
            nhapphong np = new nhapphong();
            np.Show();
            this.Close();
        }

        private void btnphongthue_Click(object sender, EventArgs e)
        {
            dt.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select  pid[Số phòng],ptang[Tầng],pdientich[Diện tích],phuongnha[Hướng nhà],pthongtin[Thông tin] from phong where pstatus = 1";

            da.SelectCommand = command;
            da.Fill(dt);
            showphong.DataSource = dt;
        }

        private void btnthontin_Click(object sender, EventArgs e)
        {
            thongtinphong pdt = new thongtinphong();
            pdt.Show();
            this.Close();
        }

        
    }
}
